package android.FiltrosJava;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

public class DrawBackGround extends AbstractImageBasic{
	protected BufferedImage background = null;	
	public DrawBackGround(BufferedImage img){
		setBackGround(img);
	}
	
	public void setBackGround(BufferedImage img){
		background = img;		
	}
	
	public BufferedImage applyFilter(BufferedImage src) {
		//fazer tratamento de escala da imagem...
		/*
		BufferedImage p = new BufferedImage( src.getWidth()+20, src.getHeight()+20, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = p.createGraphics();
		g.fillRect(0, 0, src.getWidth()+20, src.getHeight()+20);
		g.drawImage( src, 10, 10, null );		
    	g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
    	*/
		
		DrawWhiteBorder border = new DrawWhiteBorder();
		BufferedImage p = border.applyFilter(src);
		//trata a escala...
		if ((p.getHeight() > background.getHeight()/2.0f) || ((p.getWidth() > background.getWidth()/2.0f))){
			double perc;
			if ((background.getHeight()/p.getHeight()) > (background.getWidth()/p.getWidth()))
				perc = background.getWidth()/(2.0f * p.getWidth());
			else
				perc = background.getHeight()/(2.0f * p.getHeight());
			
			ScaleTransform escala = new ScaleTransform(perc,perc);
			p = escala.applyFilter(p);
		}
				
    	RotateTransform f = new RotateTransform(15);
    	p = f.applyFilter(p);
    	
    	BufferedImage pback = new BufferedImage( background.getWidth(), background.getHeight(), BufferedImage.TYPE_INT_ARGB);
    	Graphics2D gback = pback.createGraphics();
		gback.drawImage( background, 0, 0, null );		
    	gback.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
    	gback.drawImage( p, 20, 20, null );
    	
		return pback;		
	}
}